﻿using SheepStorage.Domain.Entity;
using SheepStorage.Repository.Interface;
using SheepStorage.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SheepStorage.Service.DataImp
{
    public class SheepMomHistoryServer:ISheepMomHistoryService
    {

        IBaseRepository<SheepMomHistory> _mhres;

        public SheepMomHistoryServer(IBaseRepository<SheepMomHistory> mhres)
        {
            _mhres = mhres;
        }

        /// <summary>
        /// 种母历史记录列表
        /// </summary>
        /// <param name="stadate"></param>
        /// <param name="enddate"></param>
        /// <param name="overbit"></param>
        /// <returns></returns>
        public async Task<List<SheepMomHistory>> ShowSheepMomHistory(string? stadate, string? enddate, string? overbit)
        {
            var list=_mhres.GetAll().Where(x=>x.IsDel!=1).ToList();
            if (!string.IsNullOrEmpty(stadate))
            {
                list = list.Where(x =>  x.MomHistoryDate >= DateTime.Parse(stadate)).ToList();
            }
            if (!string.IsNullOrEmpty(enddate))
            {
                list = list.Where(x => x.MomHistoryDate <= DateTime.Parse(enddate)).ToList();
            }
            if (!string.IsNullOrEmpty(overbit))
            {
                list = list.Where(x => x.OverBit == overbit).ToList();
            }
            list = list.OrderByDescending(x => x.MomHistoryId).ToList();
            return list;
        }

        public async Task<int> AddSheepMomHistory(SheepMomHistory input)
        {
            input.AddTime=DateTime.Now;
            return _mhres.Add(input);
        }

        /// <summary>
        /// 批量逻辑删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> DelSheepMomHistory(string ids)
        {
            var list=ids.Split(',');
            int[] id=list.Select(x=>int.Parse(x.Trim())).ToArray();
            var tolist = _mhres.GetAll().Where(x => id.Contains(x.MomHistoryId)).ToList();
            foreach(var item in tolist)
            {
                item.IsDel = 1;
            }
            return _mhres.UpdateRange(tolist);
        }


    }
}
